//
//  MHCHHotelViewController.m
//  Go!
//
//  Created by rimi on 16/7/8.
//  Copyright © 2016年 MHCH. All rights reserved.
//

#import "MHCHHotelViewController.h"
#import "NetWorkManager.h"
#import <CoreLocation/CoreLocation.h>
#import "MHCHHotelTableViewCell.h"
#import "NetWorkManager+HotelNetWorkManager.h"
#import "MHCHChoisCityViewController.h"
#import "MHCHCitysModel.h"
#import "MHCHChoisTimeView.h"
#import "MHCHHotelNameView.h"
#import "MHCHBackImageView.h"
#import "HotelTools.h"
#import "W_DetailViewController.h"
#import "MHCHChoissort.h"
#import "MJRefresh.h"
#import "UIImage+GIF.h"
#import <SDWebImageManager.h>
#import "UIImageView+WebCache.h"
//414 , 736
#define myhight 736
#define mainColor ColorWith(100, 100, 100)
#define btnColor ColorWith()
#define main2Color ColorWith(210, 210, 210)
#define redColors [UIColor orangeColor]
#define mywidth [UIScreen mainScreen].bounds.size.width
#define myhigh [UIScreen mainScreen].bounds.size.hight
//#define redColors ColorWith(230, 54, 37)


 static NSString *identifierone = @"cellone";

@interface MHCHHotelViewController ()<CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource,CalenderViewDelegate,HotelNameViewDelegate,MHCHChoissortDelegate>{
    
    float lat; /**< 纬度 */
    float lon; /**< 经度 */
    
}
@property (nonatomic, strong) CLLocationManager *locationManager; /**< 地图 */
@property (nonatomic, strong) UISegmentedControl *hotelSegmentedControl; /**< 分段控制器 */
@property (nonatomic, strong) UIView *timeCityAndHotelView;  /**< 城市 入住时间 地址 底图 */
@property (nonatomic, strong) UIView *screenView;  /**< 筛选 底图 */
@property (nonatomic, strong) UITableView * showTableView;
@property (nonatomic, strong) MHCHHotelTableViewCell * hotelTableViewCell;
@property (nonatomic, assign) NSInteger numberOfallday; /**< 判断是否为全日房0为全日 */
//lable
@property (nonatomic, strong) UILabel_Font *choisCityLable; /**< 目的城市 */
@property (nonatomic, strong) UILabel_Font *goinHotelTimeLable; /**< 入住时间 */
@property (nonatomic, strong) UILabel_Font *gooutHotelTimeLable;  /**< 离店时间 */
@property (nonatomic, strong) UILabel_Font *hotelDressLable;  /**< 酒店名/地址 */
@property (nonatomic, strong) UILabel_Font *choisDressLable;  /**< 选择了的地址 */
@property (nonatomic, strong) UILabel_Font *hourlyHotelLable;  /**< 钟点房时间选择 */
//btn
@property (nonatomic, strong) UIButton *normalChoisTimeBtn; /**< 普通状态下时间选择 */
@property (nonatomic, strong) UIButton *hourlyHotelChoisTimeBtn; /**< 钟点房按钮 */
@property (nonatomic, strong) UIButton *choisNearbyBtn; /**< 附近搜索按钮 */
@property (nonatomic, strong) UIButton *choisSortBtn ;  /**< 排序按钮 */
@property (nonatomic, strong) UIButton *choisScreening;  /**< 筛选按钮 */
@property (nonatomic, strong) NSArray *choisBtnArray; /**< 筛选功能栏按钮 */
//imageview
@property (nonatomic, strong) UIImageView *choisNearbyImageView; /**< 附近三角 */
@property (nonatomic, strong) UIImageView *choisSortImageView; /**< 排序三角 */
@property (nonatomic, strong) UIImageView *choisScreeningImageView; /**< 筛选三角 */
@property (nonatomic, strong) NSMutableArray *choisImageViewArray; /**< 选择三角的数组 */
@property (nonatomic, copy) NSString *choisCityStr;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *backViewTwo;
//日历相关内容处理
@property (nonatomic, strong) MHCHChoisTimeView *choistimeview; /**< 日历视图 */
@property (nonatomic, retain) NSDate *todaydate; /**< 给定今天的的date */
@property (nonatomic, retain) NSDate *tomorrowdate;  /**< 给定明天的date */
@property (nonatomic, retain) NSDate *choisdate;
@property (nonatomic, strong) NSString *todaydateStr; /**< 给定今天的的date */
@property (nonatomic, strong) NSString *tomorrowdateStr;  /**< 给定明天的date */
@property (nonatomic, strong) NSString *choisdateStr;

//酒店名
@property (nonatomic, strong) MHCHHotelNameView *hotelNameView;
@property (nonatomic, strong) MHCHBackImageView *backImageView; /**< 背景图 */
@property (nonatomic, strong) NSString *gotohotelUrl; /**<  */
@property (nonatomic, assign) BOOL isnextnetwork; /**< 是否进行下一次网络请求 */
@property (nonatomic, assign) BOOL isoncenetwork; /**< 是否为第一次网络请求 */
@property (nonatomic, assign) BOOL isgetdown; /**< 是否为下拉刷新 */
@property (nonatomic, strong) NSString *backcityid; /**< 从选择城市的地方返回的城市id */
@property (nonatomic, assign) NSInteger maxpage; /**< 数据量的最大数量 */
@property (nonatomic, assign) NSInteger choisradius; /**< 选择搜索范围 */
@property (nonatomic, assign) NSInteger choissort; /**< 选择排序方式 */
@property (nonatomic, strong) NSString * conditionsForscreening; /**< 筛选条件 */
@property (nonatomic, strong) NSString * starhotel; /**< 星级酒店 */
@property (nonatomic, strong) MHCHChoissort *choistableview; /**< 选择tableview */
@property (nonatomic, strong) NSDictionary *backsoredict; /**< 返回排序的字典 */
@property (nonatomic, strong) NSDictionary *backneberdict; /**< 返回距离的字典 */
@property (nonatomic, strong) NSDictionary *backchoisdict;  /**< 返回筛选的结果 */
@property (nonatomic, strong) NSDictionary *clickhoteldict;  /**< 钟点房传的字典 */
@property (nonatomic, assign) BOOL isalldayHotel; /**< 是否为全日房 */
//返回数据数组
@property (nonatomic, strong) NSMutableArray *dateArray;
//请求参数
@property (nonatomic, strong) NSString *currentDress;

@end

@implementation MHCHHotelViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hotelSegmentedControl.hidden = NO;
    self.isnextnetwork = YES;
    self.isoncenetwork = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isnextnetwork = NO;
    self.isoncenetwork = YES;
    self.isalldayHotel = YES;
    self.isgetdown = NO;
    self.choisradius = 100;
    self.choissort = 100;
    [self executionNetWorkingManagerForHotel:1];
    [self initializeDataSource];
    [self initializeUserInterface];
    self.navigationItem.title = @"";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)initializeDataSource/**< 初始化数据源 */{
    [self addCLLocationManager]; /**< 添加地图定位 */
    self.choisImageViewArray = [NSMutableArray array];
    self.currentDress = [[NSUserDefaults standardUserDefaults] objectForKey:currentCity_ID];
    self.todaydate = [NSDate date];
    self.tomorrowdate = [self nextDayWithDate:[NSDate date]];
}
-(void)initializeUserInterface/**< 初始化用户界面 */{
    [self.navigationController.view addSubview:self.hotelSegmentedControl]; /**< 分段控制器 */
    [self.view addSubview:self.timeCityAndHotelView]; /**< 城市 时间 地址 */
    [self.view addSubview:self.screenView];  /**< 筛选功能 */
    [self.view addSubview:self.showTableView];  /**< 显示内容 */
    self.showTableView.hidden = YES;//保证第一次刷新不会出现下拉效果
    [self addOthers];
    
}


#pragma mark - CLLocationManager

- (void)addCLLocationManager{
    _locationManager = [[CLLocationManager alloc] init];
    //定位管理器
    if ([CLLocationManager locationServicesEnabled]==NO) {
        NSLog(@"定位服务当前可能尚未打开,请设置打开");
        return;
    }
    //如果没有授权请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [_locationManager requestWhenInUseAuthorization];
    }else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
        //设置代理
        _locationManager.delegate = self;
        //设置定位精度
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //设置定位频率
        CLLocationDistance distance = 10.0;
        _locationManager.distanceFilter = distance;
        //启动跟踪定位
        [_locationManager startUpdatingLocation];
    }

}

#pragma mark - 按键上面的lable

- (void)addOthers{
    //不会改变的lable
    [self.view addSubview:[self addLableWithframe:CGRectMake(Size_W(30), Size_H(11)+ 64,Size_W(80), Size_H(22)) font:15 text:@"目的城市" textColor:ColorWith(170, 170, 170)]];
    [self.normalChoisTimeBtn addSubview:[self addLableWithframe:CGRectMake(Size_W(25), Size_H(10),Size_W(40), Size_H(22)) font:15 text:@"入住" textColor:ColorWith(170, 170, 170)]];
    [self.normalChoisTimeBtn addSubview:[self addLableWithframe:CGRectMake(Size_W(25), Size_H(35),Size_W(40), Size_H(22)) font:15 text:@"离店" textColor:ColorWith(170, 170, 170)]];
    
    //需要改变位置的lable
    self.choisCityLable = [self addLableWithframe:CGRectMake(Size_W(0), Size_H(36) + 64,(mywidth-4)/3, Size_H(28)) font:20 text:@"无法定位" textColor:[UIColor blackColor]];
    
    //网络请求获取当前城市
    self.choisCityLable.text = [HotelTools hotelWithCurrentCity];
    NSString *currecity = self.choisCityLable.text;
    [[NSUserDefaults standardUserDefaults] setObject:currecity forKey:@"cityname"];
    self.choisCityLable.textAlignment = NSTextAlignmentCenter;
    
    //全日房类的时间选择
    self.goinHotelTimeLable = [self addLableWithframe:CGRectMake(Size_W(70), Size_H(10),Size_W(80), Size_H(22)) font:15 text:@"XX-XX" textColor:ColorWith(170, 170, 170)];
    self.goinHotelTimeLable.text = self.todaydateStr;
    self.gooutHotelTimeLable = [self addLableWithframe:CGRectMake(Size_W(70), Size_H(35),Size_W(80), Size_H(22)) font:15 text:@"XX-XX" textColor:ColorWith(170, 170, 170)];
    self.gooutHotelTimeLable.text = self.tomorrowdateStr;
    
    //钟点房的时间选择
    [self.hourlyHotelChoisTimeBtn addSubview:[self addLableWithframe:CGRectMake(Size_W(20), Size_H(22),Size_W(20), Size_H(22)) font:15 text:@"住" textColor:ColorWith(170, 170, 170)]];
    self.hourlyHotelLable =[self addLableWithframe:CGRectMake(Size_W(60), Size_H(22),Size_W(80), Size_H(22)) font:15 text:@"XX-XX" textColor:ColorWith(170, 170, 170)];
    self.hourlyHotelLable.text = self.todaydateStr;
    self.hotelDressLable = [self addLableWithframe:CGRectMake(Size_W(320), Size_H(26) + 64,Size_W(90), Size_H(22)) font:15 text:@"酒店名" textColor:ColorWith(170, 170, 170)];
    //添加到视图
    [self.view addSubview:self.choisCityLable];
    [self.normalChoisTimeBtn addSubview:self.goinHotelTimeLable];
    [self.normalChoisTimeBtn addSubview:self.gooutHotelTimeLable];
    [self.view addSubview:self.hotelDressLable];
    [self.hourlyHotelChoisTimeBtn addSubview:self.hourlyHotelLable];
    
    
}

//下一天

- (NSDate *)nextDayWithDate:(NSDate *)date {
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = 1;
    //得到下一个天
    NSDate *nextDate = [calender dateByAddingComponents:components toDate:date options:0];
    
    return nextDate;
}

#pragma mark - CoreLocation 代理

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{
    //获取最新定位信息
    CLLocation * location = locations.lastObject;
    NSLog(@"%f-%f",location.coordinate.longitude,location.coordinate.latitude);
    
    //停止定位
    [_locationManager stopUpdatingLocation];
}

#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dateArray.count ;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MHCHHotelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierone];
    NSURL *imageurl = [NSURL URLWithString:self.dateArray[indexPath.row][@"deals"][0][@"image"]];
    [cell.hotelimageview sd_setImageWithURL:imageurl placeholderImage:[UIImage imageNamed:@"等待中"]];
    cell.hotelNameLable.text = self.dateArray[indexPath.row][@"deals"][0][@"title"];
    NSInteger money = [self.dateArray[indexPath.row][@"deals"][0][@"current_price"] integerValue];
    cell.hotelPiceLable.text = [NSString stringWithFormat:@"%ld",money/100];
    cell.beginTimeLable.text = self.dateArray[indexPath.row][@"deals"][0][@"description"];
    NSInteger originalMoney = [self.dateArray[indexPath.row][@"deals"][0][@"market_price"] integerValue];
    cell.originalprice.text = [NSString stringWithFormat:@"%ld",originalMoney/100];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *myfootView = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(0),Size_W(414), Size_H(2))];
    myfootView.hidden = NO;
    return myfootView;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *shopid = self.dateArray[indexPath.row][@"shop_id"];
    NSString *imageurl =  self.dateArray[indexPath.row][@"deals"][0][@"tiny_image"];
    [NetWorkManager getWithURL:@"shopinfo" urlParam:@{@"shop_id":shopid} successBlock:^(id responseObject) {
        NSLog(@"%@",responseObject);
        W_DetailViewController *secendeVC = [[W_DetailViewController alloc]init];
        secendeVC.imageURL = imageurl;
        secendeVC.detailInfo = responseObject[@"shop"];
        [self.navigationController pushViewController:secendeVC animated:YES];
        self.hotelSegmentedControl.hidden = YES;
    } FailureBlock:^(NSError *error) {
        NSLog(@"网络异常失败");
    }];
    
}

#pragma mark - CalenderViewDelegate

-(void)selecetedGoinCellWithDate:(NSDate *)date{
    self.todaydate = date;
}

-(void)selecetedGoOutCellWithDate:(NSDate *)date{
    self.tomorrowdate = date;
}

-(void)selecetedoneDayWithDate:(NSDate *)date{
    self.choisdate = date;
}

-(void)removeTheCanlenView{
    self.backView.hidden = YES;
    self.backView = nil ;
    self.choistimeview.hidden = YES;
    self.choistimeview = nil;
}

#pragma mark - HotelNameViewDelegate

-(void)backTheHotelNameForputin:(NSString *)putinStr{
    self.hotelDressLable.frame = CGRectMake(Size_W(320), Size_H(11)+64,Size_W(90), Size_H(22));
    self.choisDressLable.text = putinStr;
    self.choisDressLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.choisDressLable];
    self.backImageView.hidden = YES;
    self.backImageView = nil;
    self.hotelNameView.hidden = YES;
    self.hotelNameView = nil;
    [self executionNetWorkingManagerForHotel:1];
}

#pragma mark - AFNetWorking
//酒店网络请求无主要关键字

-(void)executionoriginalNetWorkingManagerForHotel{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *sort = [NSString stringWithFormat:@"%ld",self.choissort];
    [dict setObject:sort forKey:@"sort"];
    [dict setObject:@"1" forKey:@"page"];
    [dict setObject:@"10" forKey:@"page_size"];
    [dict setObject:@"377" forKey:@"cat_ids"];
    [dict setObject:@"642" forKey:@"subcat_ids"];
    //开启下拉刷新
//    [self.showTableView.header beginRefreshing];

        [NetWorkManager getWithURL:@"searchshops" urlParam:dict successBlock:^(id responseObject) {
        self.dateArray = nil;
        NSLog(@"responseObject=%@",responseObject[@"errno"]);
        NSInteger iserrno =[responseObject[@"errno"] integerValue];
        if (iserrno == 0) {
            self.dateArray = [NSMutableArray arrayWithArray:responseObject[@"data"][@"shops"]];
            NSLog(@"%@",self.dateArray);
            self.maxpage = [responseObject[@"data"][@"total"] integerValue];
            self.maxpage = self.maxpage/10;
            [self.showTableView reloadData];
        }else{
            [Tools showAlertWithMessage:@"没能找到你搜索的店铺"];
            self.choisDressLable.text = @"";
            [self executionoriginalNetWorkingManagerForHotel];
        }
        [self.showTableView.mj_header endRefreshing];
                self.view.userInteractionEnabled = YES;
    } FailureBlock:^(NSError *error) {
                self.view.userInteractionEnabled = YES;
    }];
}

- (void)executionNetWorkingManagerForHotel:(NSInteger)number{
    UIView *gifbackview;
    UIImageView *gifImage;
    if (!gifImage && self.isgetdown == NO) {
        gifbackview = [[UIView alloc]initWithFrame:self.showTableView.frame];
        gifbackview.backgroundColor = [UIColor whiteColor];
        gifImage = [[UIImageView alloc] initWithImage:[UIImage windy_animatedGIFNamed:@"初次进入加载动画"]];
        gifImage.frame = CGRectMake(Size_W(100), Size_H(100),Size_W(214), Size_H(214));
        [gifbackview addSubview:gifImage];
        [self.view addSubview:gifbackview];
        gifImage.hidden = NO;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if(self.backcityid == nil){
        self.backcityid  = [[NSUserDefaults standardUserDefaults] objectForKey:currentCity_ID];
    }
    NSString *choiscityid = self.backcityid;
        [dict setObject:choiscityid forKey:@"city_id"];
    
    if (self.choisradius != 100) {


    }
    //排序方式
    if (self.choissort == 100) {
        self.choissort = 0;
    }
    
    NSString *sort = [NSString stringWithFormat:@"%ld",self.choissort];
    [dict setObject:sort forKey:@"sort"];
    [dict setObject:@"1" forKey:@"page"];
    [dict setObject:@"10" forKey:@"page_size"];
    [dict setObject:@"377" forKey:@"cat_ids"];
    [dict setObject:@"642" forKey:@"subcat_ids"];
    if (self.choisDressLable.text.length >0) {
        [dict setObject:self.choisDressLable.text forKey:@"keyword"];
    }
    if (self.starhotel.length > 0) {
        [dict setObject:@"123" forKey:@"subcat_ids"];
    }
    if (self.backchoisdict) {
        [dict setValuesForKeysWithDictionary:self.backchoisdict];
    }
    if (self.backsoredict) {
        [dict setValuesForKeysWithDictionary:self.backsoredict];
    }
    
    if (self.clickhoteldict && self.isalldayHotel == NO) {
        [dict setValuesForKeysWithDictionary:self.clickhoteldict];
    }
    //开启下拉刷新
    
    [NetWorkManager getWithURL:@"searchshops" urlParam:dict successBlock:^(id responseObject) {
        self.dateArray = nil;
        NSLog(@"responseObject=%@",responseObject[@"errno"]);
        NSInteger iserrno =[responseObject[@"errno"] integerValue];
        if (iserrno == 0) {
            self.dateArray = [NSMutableArray arrayWithArray:responseObject[@"data"][@"shops"]];
            NSLog(@"%@",self.dateArray);
            self.maxpage = [responseObject[@"data"][@"total"] integerValue];
            self.maxpage = self.maxpage/10;
            [self.showTableView reloadData];
        }else{
            [Tools showAlertWithMessage:@"没能找到你搜索的店铺"];
            self.choisDressLable.text = @"";
            [self executionNetWorkingManagerForHotel:1];
        }
        self.showTableView.hidden = NO;
        [self.showTableView.mj_header endRefreshing];
        gifbackview.hidden = YES;
        [gifbackview removeFromSuperview];
                self.view.userInteractionEnabled = YES;
    } FailureBlock:^(NSError *error) {
                self.view.userInteractionEnabled = YES;
    }];
};


- (void)executiontwoNetWorkingManagerForHotel:(NSInteger)number{
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if(self.backcityid == nil){
        self.backcityid  = [[NSUserDefaults standardUserDefaults] objectForKey:currentCity_ID];
    }
    NSString *choiscityid = self.backcityid;
    [dict setObject:choiscityid forKey:@"city_id"];
    
    if (self.choisradius != 100) {
    }
    //排序方式
    if (self.choissort == 100) {
        self.choissort = 0;
    }
    NSString *sort = [NSString stringWithFormat:@"%ld",self.choissort];
    
    [dict setObject:sort forKey:@"sort"];
    [dict setObject:@"10" forKey:@"page_size"];
    [dict setObject:@"377" forKey:@"cat_ids"];
    [dict setObject:@"642" forKey:@"subcat_ids"];
    [dict setObject:[NSString stringWithFormat:@"%ld",number] forKey:@"page"];
    if (self.choisDressLable.text.length >0) {
        [dict setObject:self.choisDressLable.text forKey:@"keyword"];
    }
    if (self.starhotel.length > 0) {
        [dict setObject:@"123" forKey:@"subcat_ids"];
    }
    if (self.backchoisdict) {
        [dict setValuesForKeysWithDictionary:self.backchoisdict];
    }
    if (self.backsoredict) {
        [dict setValuesForKeysWithDictionary:self.backsoredict];
    }
    if (self.clickhoteldict && self.isalldayHotel == NO) {
        [dict setValuesForKeysWithDictionary:self.clickhoteldict];
    }
    [self.showTableView.mj_footer beginRefreshing];
    [NetWorkManager getWithURL:@"searchshops" urlParam:dict successBlock:^(id responseObject) {
        
        NSArray *array = [NSArray arrayWithArray:responseObject[@"data"][@"shops"]];
        [self.dateArray addObjectsFromArray:array];
        NSLog(@"%ld",self.dateArray.count);
        self.showTableView.userInteractionEnabled = YES;
        self.isnextnetwork = YES;
        [self.showTableView reloadData];
         [self.showTableView.mj_footer endRefreshing];
                self.view.userInteractionEnabled = YES;
    } FailureBlock:^(NSError *error) {
                self.view.userInteractionEnabled = YES;
    }];



}

#pragma mark - respondToBtn

-(void)loadNewData{
    self.view.userInteractionEnabled = NO;
    self.isgetdown = YES;
    [self executionNetWorkingManagerForHotel:1];
}

-(void)loadMoreData{
        self.view.userInteractionEnabled = NO;
    [self executiontwoNetWorkingManagerForHotel:1];
}

- (void)change:(UISegmentedControl *)segmentControl{
    if (segmentControl.selectedSegmentIndex == 1) {
        self.hourlyHotelChoisTimeBtn.hidden = NO;
        self.numberOfallday = 0;
    }else{
        self.hourlyHotelChoisTimeBtn.hidden = YES;
        self.numberOfallday = 1;
    }
    
    if (segmentControl.selectedSegmentIndex == 0) {
        NSLog(@"全日房间");
        self.isalldayHotel = YES;
        [self executionNetWorkingManagerForHotel:1];
    }else if (segmentControl.selectedSegmentIndex == 1) {
        NSLog(@"钟点房");
        self.isalldayHotel = NO;
        self.clickhoteldict = @{@"subcat_ids":@"943"};
         [self executionNetWorkingManagerForHotel:1];
    }else if (segmentControl.selectedSegmentIndex == 2) {
        NSLog(@"青年宿舍");
    }else if (segmentControl.selectedSegmentIndex == 3) {
        NSLog(@"星级酒店");
    }
}

- (void)respondsToChoiseBtn:(UIButton *)sender{
    if (sender.tag == 1000) {
        NSLog(@"选择城市");
        self.hotelSegmentedControl.hidden = YES;
        MHCHChoisCityViewController *choisCityVC = [[MHCHChoisCityViewController alloc]init];
        [self.navigationController pushViewController:choisCityVC animated:YES];
        choisCityVC.theCityName = self.choisCityLable.text;
        
        //实现目的城市lable动画
        choisCityVC.animationBlock = ^(NSString *choisCityName , NSString *cityidstr){
            self.choisCityStr = choisCityName;
            self.backcityid = cityidstr;
            
        };
        
    }else if (sender.tag == 1001){
        NSLog(@"打开日历");
        [self.view addSubview:self.backView];
        [self.view addSubview:self.choistimeview];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isgoin"];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isonce"];
    }else if (sender.tag == 1002){
        NSLog(@"酒店地址");
        [self.view addSubview:self.backImageView];
        [self.view addSubview:self.hotelNameView];
//        self.hotelSegmentedControl.hidden = YES;

    }else if(sender.tag == 1003){
        NSLog(@"打开第二日历");
        [self.view addSubview:self.backView];
        [self.view addSubview:self.choistimeview];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isgoin"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isonce"];
    }

}

- (void)respondsToChoiseBtnWithChois:(UIButton *)sender{
    UIButton *btn;
    UIImageView *imageview;
    for (int i = 0; i < 3; i++) {
        if (sender == self.choisBtnArray[i]) {
            btn = self.choisBtnArray[i];
            btn.selected = YES;
        }else{
            btn = self.choisBtnArray[i];
            btn.selected = NO;
        }
        imageview = self.choisImageViewArray[i];
        //切换图片 与 添加动画
        if (btn.selected == NO) {
            imageview.image = [UIImage imageNamed:@"酒店三角-未展开.png"];
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
                imageview.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
            } completion:^(BOOL finished) {
                imageview.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
            }];
        }else{
            imageview.image = [UIImage imageNamed:@"酒店三角-展开.png"];
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
                imageview.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
            } completion:^(BOOL finished) {
                imageview.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
            }];

        }
    }
    NSArray *array;
    if (sender.tag == 1000) {
        NSLog(@"搜索附近");
        array = @[@"附近5000米",@"附近3000米",@"附近1000米"];
    }else if (sender.tag == 1001){
        NSLog(@"排序");
        array = @[@"综合排序",@"价格从低到高排序",@"价格从高到低排序",@"折扣从高到低排序",@"销量从高到低排序",@"距自己的位置从近到远排序",@"最新发布"];
    }else if (sender.tag == 1002){
        NSLog(@"筛选");
        array = @[@"五星酒店",@"四星酒店",@"三星酒店",@"舒适型酒店"];
    }
    self.choistableview.hidden = YES;
    [self.choistableview removeFromSuperview];
    self.choistableview = nil;
    for (int i = 0 ; i<3; i++) {
        UIButton *btn = self.choisBtnArray[i];
        if (btn.selected == YES) {
            self.choistableview = [[MHCHChoissort alloc]initWithframe:CGRectMake(Size_W(0), Size_H(136)+64,Size_W(414), Size_H(280)) Withdata:array];
            self.choistableview.delegate = self;
            [self addbackview];
            [self.view addSubview:self.choistableview];
            return;
        }else{
            self.choistableview = nil;
        }
    }
}

#pragma mark - 助手功能

-(void)addbackview{
    [self.view addSubview:self.backViewTwo];
}

-(void)removewbackview{
    self.backViewTwo.hidden = YES;
    [self.backViewTwo removeFromSuperview];
    self.backViewTwo = nil;
}

#pragma mark - MHCHChoissortDelegate

-(void)choissortisOkbackmessage:(NSDictionary *)backmessage{
    [self choisClossAll];
    
    NSLog(@"%@",backmessage);
    self.backsoredict = backmessage;
    [self executionNetWorkingManagerForHotel:1];
}

-(void)choisstarisOkbackmessage:(NSDictionary *)backmessage{
    self.backchoisdict = backmessage;
    [self executionNetWorkingManagerForHotel:1];
        [self choisClossAll];
}

-(void)choisnaberisOkbackmessage:(NSDictionary *)backmessage{
           [self choisClossAll];
}


#pragma mark - touchesBegan

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIButton *btn;
    
    self.backView.hidden = YES;
    self.backView = nil;
    self.backViewTwo.hidden = YES;
    self.backViewTwo = nil;
    self.choistimeview.hidden = YES;
    self.choistimeview = nil;
    self.backImageView.hidden = YES;
    self.backImageView = nil;
    self.hotelNameView.hidden = YES;
    self.hotelNameView = nil;
    self.choistableview.hidden = YES;
    [self.choistableview removeFromSuperview];
    self.choistableview = nil;

    
    for (int i = 0; i<3; i++) {
        btn = self.choisBtnArray[i];
        btn.selected = NO;
    UIImageView *imageview = self.choisImageViewArray[i];
    //切换图片 与 添加动画
    if (btn.selected == NO) {
        imageview.image = [UIImage imageNamed:@"酒店三角-未展开.png"];
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            imageview.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
        } completion:^(BOOL finished) {
            imageview.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
        }];
    }else{
        imageview.image = [UIImage imageNamed:@"酒店三角-展开.png"];
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            imageview.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
        } completion:^(BOOL finished) {
            imageview.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
        }];
      }
    }
}

-(void)choisClossAll{
    UIButton *btn;
    self.backView.hidden = YES;
    self.backView = nil;
    self.backViewTwo.hidden = YES;
    self.backViewTwo = nil;
    self.choistimeview.hidden = YES;
    self.choistimeview = nil;
    self.backImageView.hidden = YES;
    self.backImageView = nil;
    self.hotelNameView.hidden = YES;
    self.hotelNameView = nil;
    self.choistableview.hidden = YES;
    [self.choistableview removeFromSuperview];
    self.choistableview = nil;
    self.isgetdown = NO;
    for (int i = 0; i<3; i++) {
        btn = self.choisBtnArray[i];
        btn.selected = NO;
        UIImageView *imageview = self.choisImageViewArray[i];
        //切换图片 与 添加动画
        if (btn.selected == NO) {
            imageview.image = [UIImage imageNamed:@"酒店三角-未展开.png"];
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
                imageview.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
            } completion:^(BOOL finished) {
                imageview.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1);
            }];
        }else{
            imageview.image = [UIImage imageNamed:@"酒店三角-展开.png"];
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
                imageview.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
            } completion:^(BOOL finished) {
                imageview.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
            }];
        }
    }
}

#pragma mark - addbtn

- (UIButton *)addBtn:(NSInteger)btnNumber{
    UIButton *btn;
    if (btnNumber == 3) {
        btn= [[UIButton alloc]initWithFrame:CGRectMake(Size_W(414)*1/3.0, Size_H(2),Size_W(410/3.0), Size_H(62))];
    }else{
        btn = [[UIButton alloc]initWithFrame:CGRectMake(Size_W(414)*btnNumber/3.0, Size_H(2),Size_W(410/3.0), Size_H(62))];
    }
    [btn addTarget:self action:@selector(respondsToChoiseBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = ColorWith(255, 255, 255);
    btn.tag = 1000+btnNumber;
    return btn;
}

- (UIButton *)addBtnWithChoisnum:(NSInteger)btnNumber name:(NSString *)btnName{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(Size_W(414)*btnNumber/3.0, Size_H(0),Size_W(414/3.0), Size_H(50))];
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(Size_W(90), Size_H(15),Size_W(20), Size_H(20))];
    if (btn.selected == NO) {
        imageview.image = [UIImage imageNamed:@"酒店三角-未展开.png"];
    }else{
        imageview.image = [UIImage imageNamed:@"酒店三角-展开.png"];
    }

    [btn addSubview:imageview];
    //给每一个imageview转换成全局
    if (btnNumber == 0) {
        self.choisNearbyImageView = imageview;
           [self.choisImageViewArray addObject:self.choisNearbyImageView];
    }else if(btnNumber == 1){
        self.choisSortImageView = imageview;
         [self.choisImageViewArray addObject:self.choisSortImageView];
    }else if (btnNumber ==2){
        self.choisScreeningImageView = imageview;
         [self.choisImageViewArray addObject:self.choisScreeningImageView];
    }
 
    [btn addTarget:self action:@selector(respondsToChoiseBtnWithChois:) forControlEvents:UIControlEventTouchUpInside];
    //设置按钮样式
    [btn setTitle:btnName forState:UIControlStateNormal];
    [btn setTitleColor:mainColor forState:UIControlStateNormal];
    [btn setTitleColor:mainColor forState:UIControlStateHighlighted];
    [btn setTitleColor:redColors forState:UIControlStateSelected];
    [btn setTitleColor:redColors forState:UIControlStateSelected|UIControlStateHighlighted];
    btn.backgroundColor = [UIColor whiteColor];
    btn.tag = 1000+btnNumber;
    return btn;
}

#pragma mark - addlable

- (UILabel_Font *)addLableWithframe:(CGRect)frame
                          font:(CGFloat)font
                          text:(NSString *)textstr
                     textColor:(UIColor *)textcolor
{
    UILabel_Font *lable = [[UILabel_Font alloc]initWithFrame:frame];
//    lable.backgroundColor = [UIColor blackColor];
    lable.font = [UIFont systemFontOfSize:Font(font)];
    lable.text = textstr;
    lable.textColor = textcolor;
    return lable;
}

#pragma mark - SetAndGet
// 分段控制器
- (UISegmentedControl *)hotelSegmentedControl{
    if (!_hotelSegmentedControl) {
        NSArray *array = @[@"全日房",@"钟点房",];
        _hotelSegmentedControl = [[UISegmentedControl alloc]initWithItems:array];
        _hotelSegmentedControl.selectedSegmentIndex = 0;
        _hotelSegmentedControl.frame = CGRectMake(Size_W((414-350)/2), 25,Size_W(350), Size_H(40));
        _hotelSegmentedControl.tintColor = redColors;
        [_hotelSegmentedControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    }
    return _hotelSegmentedControl;
}
//选择目的城市 入住时间 离店时间 酒店地址
- (UIView *)timeCityAndHotelView{
    if (!_timeCityAndHotelView) {
        _timeCityAndHotelView = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0), 64,Size_W(414), Size_H(66))];
        _timeCityAndHotelView.backgroundColor = ColorWith(230, 230, 230);
        self.normalChoisTimeBtn = [self addBtn:1];
        self.hourlyHotelChoisTimeBtn =[self addBtn:3];
        self.hourlyHotelChoisTimeBtn.hidden = YES;
        [_timeCityAndHotelView addSubview:[self addBtn:0]];
        [_timeCityAndHotelView addSubview:self.normalChoisTimeBtn];
        [_timeCityAndHotelView addSubview:[self addBtn:2]];
        [_timeCityAndHotelView addSubview:self.hourlyHotelChoisTimeBtn];
    }
    return _timeCityAndHotelView;
}
//筛选 底图
- (UIView *)screenView{
    if (!_screenView) {
        _screenView = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0),64+ Size_H(66),Size_W(414), Size_H(66))];
        _screenView.backgroundColor = ColorWith(230, 230, 230);
        self.choisNearbyBtn = [self addBtnWithChoisnum:0 name:@"附近"];
        self.choisSortBtn = [self addBtnWithChoisnum:1 name:@"排序"];
        self.choisScreening = [self addBtnWithChoisnum:2 name:@"筛选"];
        self.choisBtnArray = @[self.choisNearbyBtn,self.choisSortBtn,self.choisScreening];
        [_screenView addSubview:self.choisNearbyBtn];
        [_screenView addSubview:self.choisSortBtn];
        [_screenView addSubview:self.choisScreening];
    }
    return _screenView;
}
//展示用tableview
- (UITableView *)showTableView{
    if (!_showTableView) {
        _showTableView = [[UITableView alloc]initWithFrame:CGRectMake(Size_W(0),64 + Size_H(132),Size_W(414), Size_H(myhight - 132)-64-49)style:UITableViewStylePlain];
        _showTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"酒店背景.png"]];
        _showTableView.delegate = self;
        _showTableView.dataSource = self;
        [_showTableView registerNib:[UINib nibWithNibName:@"MHCHHotelTableViewCell" bundle:nil] forCellReuseIdentifier:identifierone];
        _showTableView.estimatedRowHeight =100;
        _showTableView.rowHeight = UITableViewAutomaticDimension;
        _showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
        _showTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
        _showTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _showTableView;
}


- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0), 64,Size_W(414), Size_H(736)-64-49)];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.7;
    }
    return _backView;
}

- (UIView *)backViewTwo{
    if (!_backViewTwo) {
        _backViewTwo = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0), CGRectGetMinY(self.choistableview.frame),Size_W(414), Size_H(736)-64-49)];
        _backViewTwo.backgroundColor = [UIColor clearColor];
    }
    return _backViewTwo;
}

- (MHCHBackImageView *)backImageView{
    if (!_backImageView) {
        _backImageView = [[MHCHBackImageView alloc]initWithFrame:CGRectMake(Size_W(0), 64,Size_W(414), Size_H(736)-49-64)];
        _backImageView.userInteractionEnabled = YES;
        _backImageView.image = [UIImage imageNamed:@"酒店背景.jpg"];
        _backImageView.alpha = 0.9;
    }
    return _backImageView;
}

- (UIView *)choistimeview{
    if (!_choistimeview) {
        _choistimeview = [[MHCHChoisTimeView alloc]init];
        _choistimeview.delegate = self;
    }
    return _choistimeview;
}

- (MHCHHotelNameView *)hotelNameView{
    if (!_hotelNameView) {
        _hotelNameView = [[MHCHHotelNameView alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(135),self.view.bounds.size.width/1.5, Size_H(100))];
        _hotelNameView.center = self.view.center;
        _hotelNameView.delegate = self;
    }
    return _hotelNameView;
}

-(UILabel_Font *)choisDressLable{
    if (!_choisDressLable) {
        _choisDressLable = [[UILabel_Font alloc]initWithFrame:CGRectMake(Size_W(280), Size_H(36)+64,Size_W(130), Size_H(22))];
    }
    return _choisDressLable;
}


//动画set

- (void)setChoisCityStr:(NSString *)choisCityStr{
    
    _choisCityStr = choisCityStr;
    UILabel_Font *choislable = [[UILabel_Font alloc]initWithFrame:CGRectMake(Size_W(414), Size_H(400) , Size_W(414), Size_H(80))];
    choislable.backgroundColor = [UIColor orangeColor];
    choislable.text = choisCityStr;
    choislable.textAlignment = NSTextAlignmentCenter;
    choislable.font = [UIFont systemFontOfSize:Font(30)];
    [self.view addSubview:choislable];
        [UIView animateWithDuration:1.0 animations:^{
            choislable.frame = CGRectMake(Size_W(0), Size_H(400) , Size_W(414), Size_H(80));
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 animations:^{
                choislable.frame = CGRectMake(Size_W(414-120), Size_H(414),Size_W(120), Size_H(28));
                choislable.font = [UIFont systemFontOfSize:Font(18)];
            } completion:^(BOOL finished) {
                choislable.backgroundColor = [UIColor clearColor];
                [UIView animateWithDuration:1. animations:^{
                    choislable.frame = CGRectMake(Size_W(0), Size_H(36)+64,Size_W(120), Size_H(28));
                } completion:^(BOOL finished) {
                    self.choisCityLable.text = choislable.text;
                    choislable.hidden = YES;
                    [choislable removeFromSuperview];
                    [self executionNetWorkingManagerForHotel:1];
                }];
            }];
        }];
}

- (void)setTodaydate:(NSDate *)todaydate{
    _todaydate = todaydate;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM-dd";
    NSString *dateString = [formatter stringFromDate:todaydate];
    self.todaydateStr = dateString;
    self.goinHotelTimeLable.text = self.todaydateStr;
    NSLog(@"%@",dateString);
    
}

- (void)setTomorrowdate:(NSDate *)tomorrowdate{
    _tomorrowdate = tomorrowdate;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM-dd";
    NSString *dateString = [formatter stringFromDate:tomorrowdate];
    self.tomorrowdateStr = dateString;
    self.gooutHotelTimeLable.text = self.tomorrowdateStr;
    NSLog(@"%@",dateString);
}

-(void)setChoisdate:(NSDate *)choisdate{
    _choisdate = choisdate;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM-dd";
    NSString *dateString = [formatter stringFromDate:choisdate];
    self.hourlyHotelLable.text = dateString;
    NSLog(@"111%@",dateString);
}
@end
